package com.ngc.mall.util;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Random;

public class CommonUtil {
    public static String timestamp10(){
        Long timestamp13 = Calendar.getInstance().getTimeInMillis();
        String timestamp13Str = timestamp13.toString();
        return timestamp13Str.substring(0, timestamp13Str.length() - 3);
    }

    /**
     * 判断当前时间是否已过期
     * @param date
     * @param start
     * @param end
     * @return
     */
    public static Boolean isInTimeLine(LocalDateTime date, LocalDateTime start, LocalDateTime end) {
        if(date.isAfter(start)&&date.isBefore(end)){
            return true;
        }

        return false;
    }
    /**
     * 随机生成英文数字组合字符串
     */
    public static String generateByRandom(final int length) {
        StringBuilder randomSb = new StringBuilder(length);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            // 输出字母还是数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 字符串
            if ("char".equals(charOrNum)) {
                // 判断字母大小写
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                randomSb = randomSb.append((char) (choice + random.nextInt(26)));
            } else {
                randomSb = randomSb.append(random.nextInt(10));
            }
        }
        return randomSb.toString();
    }

    /**
     * 获取订单创建后的失效时间
     * @param calendar
     * @param seconds
     * @return
     */
    public static Calendar addSomeSeconds(Calendar calendar, int seconds) {
        calendar.add(Calendar.SECOND, seconds);
        return calendar;
    }
}
